package com.zxp.security.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zxp.common.core.security.authentication.MobileAuthenticationSecurityConfig;
import com.zxp.common.core.security.authentication.handler.MyAuthenticationSuccessHandler;
import com.zxp.common.core.security.authorize.ZxpAuthorizationConfigurerManager;
import com.zxp.common.core.security.authorize.impl.ZxpAuthorizationConfigurerManagerImpl;
import com.zxp.common.core.security.filter.ValidateCodeFilter;
import com.zxp.common.core.security.properties.SecurityProperties;
import com.zxp.common.core.validatecode.impl.ImageValidateCodeService;
import com.zxp.common.core.validatecode.impl.SmsValidateCodeService;
import com.zxp.security.jwt.MyJwtTokenEnhancer;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

/**
 * @author shollin
 * @desc
 * @date 2021/7/11/011 22:41
 */
@Configuration
@RequiredArgsConstructor
public class MySecurityBeanConfig {

    private final SecurityProperties securityProperties;
    private final ImageValidateCodeService imageValidateCodeService;
    private final SmsValidateCodeService smsValidateCodeService;
    private final ValidateCodeFilter validateCodeFilter;

    private final AuthenticationFailureHandler authenticationFailureHandler;
    private final PersistentTokenRepository rememberMeTokenRepository;

    private final ObjectMapper objectMapper;
    private final ClientDetailsService clientDetailsService;
    private final DataSource dataSource;
    private final UserDetailsService userDetailsService;

    /**
     * redis工厂，默认使用lettue
     */
    @Autowired
    public RedisConnectionFactory redisConnectionFactory;


    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    //处理token生成逻辑: 默认为uuid形式，见源码DefaultTokenServices的234行
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        // 设置签名密钥
        jwtAccessTokenConverter.setSigningKey(securityProperties.getJwt().getSecret());
        return jwtAccessTokenConverter;
    }

    // 自定义增强器，添加一些额外的token数据
    @Bean
    public TokenEnhancer tokenEnhancer() {
        return new MyJwtTokenEnhancer();
    }


}
